// 文件路径: com.huaxin.device.controller.DeviceReadingEnvironmentController.java
package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.DeviceReadingEnvironmentCreateReqDTO;
import com.huaxin.device.dto.request.DeviceReadingEnvironmentPageListReqDTO;
import com.huaxin.device.models.DeviceReadingEnvironment;
import com.huaxin.device.service.DeviceReadingEnvironmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "环境检测仪数据")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/reading/environment")
public class DeviceReadingEnvironmentController {

    private final DeviceReadingEnvironmentService service;

    @Operation(summary = "创建环境检测仪数据", description = "用于上报或测试创建一条环境数据记录")
    @PostMapping("/create")
    public ResponseResult<DeviceReadingEnvironment> create(@RequestBody @Valid DeviceReadingEnvironmentCreateReqDTO in) {
        return service.create(in);
    }

    @Operation(summary = "获取环境检测仪最新数据", description = "根据设备ID获取最新一条环境数据")
    @GetMapping("/latest")
    public ResponseResult<DeviceReadingEnvironment> getLatest(
            @Parameter(description = "设备实例ID", example = "d123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "deviceId", required = true) String deviceId) {
        DeviceReadingEnvironment data = service.getLatestByDeviceId(deviceId);
        return ResponseResult.ok(data);
    }

    @Operation(summary = "分页查询环境检测仪数据", description = "支持按设备ID和时间范围分页查询历史环境数据")
    @PostMapping("/page")
    public ResponseResult<IPage<DeviceReadingEnvironment>> pageQuery(
            @RequestBody @Valid DeviceReadingEnvironmentPageListReqDTO in) {
        return service.pageQuery(in);
    }
}